package cn.ll.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ll.po.Connector;
import cn.ll.po.ConnectorDAO;
import cn.ll.po.Contract;
import cn.ll.po.ContractDAO;
import cn.ll.po.Regions;
import cn.ll.po.RegionsDAO;

public class RegionsService {
	RegionsDAO regionsDAO;

	/**
	 * 存储一个Regions(地区)对象
	 * 
	 * @param contract
	 */
	public void create(Regions regions) {
		regionsDAO.save(regions);
	}

	public void update(Regions regions) {
		regionsDAO.attachDirty(regions);
	}

	/**
	 * 删除一个contract(协议)对象
	 * 
	 * @param contract
	 */
	public void delete() {
	}

	/**
	 * 
	 * @param regions
	 * @param cdt
	 * @param p
	 * @param pn
	 * @return
	 */
	public List read(Regions regions) {

		List<Regions> lst = null;

		if (regions.getLevel() == 1) {
			lst = regionsDAO.findByLevel(regions.getLevel());
		} else {
			lst = regionsDAO.findByExample(regions);
		}

		List lsRs = this.toKeyValueMap(lst);

		return lsRs;
	}
	
	public List<Regions> findByRegionname(String regionname){
		List<Regions> list = regionsDAO.findByRegionname(regionname);
		return list;
	}
	
	private List toKeyValueMap(List<Regions> lst) {

		List lstRs = new ArrayList<Map>();
		for (int i = 0; i < lst.size(); i++) {
			Regions regions = lst.get(i);
			Map mp = new HashMap();
			mp.put("key", regions.getId());
			mp.put("value", regions.getRegionname());
			lstRs.add(mp);
		}

		return lstRs;
	}

	public RegionsDAO getRegionsDAO() {
		return regionsDAO;
	}

	public void setRegionsDAO(RegionsDAO regionsDAO) {
		this.regionsDAO = regionsDAO;
	}



}
